{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的计算",
   "id": "8e76188707a7d9f1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:52:05.831234Z",
     "start_time": "2025-08-27T06:52:05.720613Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "8acf893a2a2e1c3e",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.1 数组和标量之间的计算 数组中的每个元素都会与标量进行运算",
   "id": "f05663330cc381f0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:55:58.646884Z",
     "start_time": "2025-08-27T06:55:58.637002Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.arange(10)\n",
    "print(arr1)\n",
    "# 加法\n",
    "print(arr1 + 1)\n",
    "# 减法\n",
    "print(arr1 - 1)\n",
    "# 乘法\n",
    "print(arr1 * 5)\n",
    "# 除法\n",
    "print(arr1 / 2)\n",
    "\n",
    "# 二次幂\n",
    "print(arr1 ** 2)"
   ],
   "id": "8e0ce1cf3063aaa0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[ 1  2  3  4  5  6  7  8  9 10]\n",
      "[-1  0  1  2  3  4  5  6  7  8]\n",
      "[ 0  5 10 15 20 25 30 35 40 45]\n",
      "[0.  0.5 1.  1.5 2.  2.5 3.  3.5 4.  4.5]\n",
      "[ 0  1  4  9 16 25 36 49 64 81]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.2 \t相同形状数组的计算，对应位置的元素进行计算，假设数组a和数组b，可以直接用a+b 或 a-b\n",
   "id": "2f564de2559869b3"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 1.2.1 a+b:对应位置的数字进行计算(加减乘除)",
   "id": "7e9c2ecf5b07ec14"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:59:20.134350Z",
     "start_time": "2025-08-27T06:59:20.126646Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.arange(1,10)\n",
    "arr3 = np.arange(11,20)\n",
    "\n",
    "print(arr2,arr2.shape)\n",
    "print(arr3,arr3.shape)\n",
    "\n",
    "print('=' * 10)\n",
    "# 加法\n",
    "print(arr2 + arr3)\n"
   ],
   "id": "98d92011d4bf7238",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8 9] (9,)\n",
      "[11 12 13 14 15 16 17 18 19] (9,)\n",
      "==========\n",
      "[12 14 16 18 20 22 24 26 28]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "SqlCellData": {
     "variableName$1": "df_sql1"
    }
   },
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "%%sql\n",
   "id": "19c81cf06f3fa638"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.2.2 一维数组和多维数组的计算\n",
    "- 形状一样的数组按对应位置进行计算\n",
    "- 一维数组和多维数组是可以计算的，只要他们在某一维度上是一样的形状，仍然是按位置计算\n"
   ],
   "id": "2f099ed1625e5c61"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T07:13:36.561813Z",
     "start_time": "2025-08-27T07:13:36.552969Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(6)\n",
    "b = np.arange(24).reshape(4,6)\n",
    "\n",
    "print(a)\n",
    "print('-' * 30)\n",
    "print(b)\n",
    "print('=' * 30)\n",
    "\n",
    "print('a的形状是:',a.shape)\n",
    "print('b的形状是:',b.shape)\n",
    "arr4 = a + b\n",
    "print(arr4)"
   ],
   "id": "98229cf5472d75b7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "------------------------------\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "==============================\n",
      "a的形状是: (6,)\n",
      "b的形状是: (4, 6)\n",
      "[[ 0  2  4  6  8 10]\n",
      " [ 6  8 10 12 14 16]\n",
      " [12 14 16 18 20 22]\n",
      " [18 20 22 24 26 28]]\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
